package com.huiminpay.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class PayTestController {

    // 172.16.17.135:56050/transaction/alipaytest    必须书写Ip,不能书写localhost
    @GetMapping("/alipaytest")
    public void alipayTest(HttpServletResponse httpResponse) throws IOException {
        //支付宝网关地址  测试使用  https://openapi.alipaydev.com/gateway.do  正式的:https://openapi.alipay.com/gateway.do
        String serverUrl = "https://openapi.alipaydev.com/gateway.do";
        //沙箱应用Id
        String APP_ID = "2021000121613030";
        //应用私钥
        String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCvNSWiTwAos5is5lViqxLI3IrWMVSKRFCyXq10XLrgmbKnk7P3VZgUSwsUcIXW6/8pb2JB4ycDMgFvyNPfOAxf2oeBq3AWFbRUWjRGKzJSa/jaKGuhN8A9tvvAAAAAhSrOzBsQeVvImDlxY07/0l5PA3qoy/R2uGHenC0EroCxrB/RiE6fZwwYGCijfAEAYjIC4s3jAYI/cb7PDn2xTk2w5queY75k2wMFLmdELeyvEaspUaYIwp07pYUY4TP/12/3QFtvEPgD8UcwsL7Ghgvr01QoImRfDWSUmBgZFVoFAzHmz+Jh5h1VxMADp8WlbmH3CReBwXM2xtRsvC2GCqxnAgMBAAECggEAdkWaMgbCRLd3yL9zxDdXZt/5mL2UJAIZt07SKEqwnA0k3JqIUd4l2w0NXa7Xqk2E4aOe4HtGSpHnBl67J2PedpY+AvqHn2Dnakm6H+PpBFp+kkXW4e2QiBiNuP5HAhLi4tZFLsm9e5vGqxQur+KCZzQiCPAlBj9RUcR7Xz2tAkHe8b/fG40EwytEC3p3idEb4KSgQBHEs+ck6KeYc+ZDNV4p2tK6dQDeGaimWsfINnzoY3s8TotPKU9ZfwecZ83jpaYd2so50f830OQuiEF9CxtcSTNIc3mqL01FH395oSxYcAT09Shg8UshvfVLysgsyWPTWNpqsGL/b/PFMn4PAQKBgQDvB4P4Za3nb/KVUIDuddOvFwIh78ZjvmYcRVQRsoIfNUVy0Efcw4IoZ4H/zqJCgx87c3vwNoCi8CHdi3RfOBnl5ZJc0OhbGMl+e9kEN+YbkL2ajfZKBdJCXrMslEYFSZ/YP+8s4/4UNruOaztatTSetQtqKhf8ZH84VAPHhPYfzwKBgQC7paM1kbPFejlY3QjxXuYQkVNhZCyOOPHwCJdTTQpD/PbeS/PyQEBksXKhZejJ8ErFEn4IWfHPxXVhSgQkl7iomhhDT7EO0jKPTXKJIwoyY6WPiraCYEFZtc4GkpQdo759372XEKuiYzOsT8uh12PAv7gSR8AI3AO+zJicl/n36QKBgFn0oh3hZkkrQEghO+SCQ2ApcIwFCMyQ4yzQior+eynzimdjKyhSC7HZAnua8EXHwwFEvOfyGwc3CAewa+NJ4bU7aJG1rTxbX7lSMqH7x4WoOzLnKkFBX5fKu/OS+acTxixEI2bphr1ku8ElafkBKQRxbhJONSivSZGRTLrRty7RAoGAEH1CtU9ja8QMzAlIYuWC9AvBNvq1i53nbL9khNZtQ4I0M16bRjttLSwGtbYd2bj66Qxx9MvqmuZwJSd667hFatN3tFqJZZMFDGCy4ugoWjs1bkKzPQu2UmrOevdgeqZL9AW4RFEbs/5hqQ+uBj/GLUbrpveVIXIsBhU2h/Q27BkCgYEAyCRrIIfarYM9mvaKwxi0Z2+5fCQArkXIzhZrpBgI7Eo0svGi5kW4bZbadxgDBXlSeY726ce55BOl0kFFEedW/qXOEoQrUCi/8PrbJt6qEs0K2C/47eRk1m/KhrhdItMMqjNA7CgFmtaPqKileJYqp0aP8Qnnj/jELCAj+RgjUTQ=";
        //编码方式
        String CHARSET = "UTF-8";
        //支付宝公钥
        String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlwmUoocEpVm54QaspsDIZHfL8R7uI1EZpLfLALu2rR/u9Mi0M9ohX8hb3ldGMqqHSGIfsHnj6+lDGEMWIfFVZtF1gvEjWFqarrJ1pwFsNlG235Jb3IP9hs7DPjJFjyMgD9tPKn0ik7i+chKWelbMFHMZdzJyx/izXZLudWRo60Ps79dzjC+9J3Unzvrep0grbdAv0WD5hC11nbSzjNWGuY+fJy/pTNwt0aaEW5QqWvQrox1cfcAo1N5XVepzkual6EuRyxN1//jS3reQryxj0htNCoGgEostDlvALXwN0GtumhgjcXeljYa6z2o7eM5oa18E7WWIOLI84gCzDWhquQIDAQAB";
        //签名类型
        String signType = "RSA2";

        //构造支付宝客户端对象
        AlipayClient alipayClient = new DefaultAlipayClient(
                serverUrl,
                APP_ID, APP_PRIVATE_KEY, "json",
                CHARSET, ALIPAY_PUBLIC_KEY, signType); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
//        alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址

        //填充业务参数
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"20,0320010101002\"," +
                " \"total_amount\":\"88.88\"," +
                " \"subject\":\"Iphone13 Pro Max 512G  远峰蓝\"," +
                " \"product_code\":\"QUICK_WAP_WAY\"" +
                " }");
        String form = "";
        try {
            // 调用SDK生成表单
            form = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        //直接将完整的表单html输出到页面
        httpResponse.getWriter().write(form);
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }

}
